package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.MonitorInfoPAdapter;
import com.hxkcloud.patient.beans.PatientAssessBean;
import com.hxkcloud.patient.beans.PatientAssessWholeBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.UrlAddress;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-08-06.
 * Description:消息，评估提醒patient
 */
public class EvaluateInfoPActivity extends BaseActivity implements View.OnClickListener, MonitorInfoPAdapter.onItemSelectListener, PullToRefreshLayout.OnRefreshListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private LinearLayout ll_empty;
    private TextView tv_empty;

    private PatientAssessWholeBean wholeBean;
    private int totalCount = 0;
    private String patientId;
    private List<PatientAssessBean> monitorList;
    private MonitorInfoPAdapter adapter;

    private int pageIndex = 1;
    private int pageSize = 15;
    private int loadmoreType = 0;//0第一次获取1刷新2更多

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETPATIENTASSESS_SUCCESS:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    monitorList.clear();
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                } else {
                    monitorList.clear();
                }
                if (msg.obj != null) {
                    wholeBean = (PatientAssessWholeBean) msg.obj;
                    totalCount = wholeBean.getTotalCount();
                    patientId = wholeBean.getPatientId();
                    if (wholeBean.getList() != null && wholeBean.getList().size() > 0) {
                        monitorList.addAll(wholeBean.getList());
                        adapter.setData(monitorList);
                    }
                } else {
                    toastMsg(R.string.datagetfail);
                }

                if (monitorList == null || monitorList.size() == 0) {
                    ll_empty.setVisibility(View.VISIBLE);
                }
                break;
            case MessageCode.NETWORKCODE_GETPATIENTASSESS_FAILURE:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }

                if (monitorList == null || monitorList.size() == 0) {
                    ll_empty.setVisibility(View.VISIBLE);
                }
                break;
            case MessageCode.NETWORKCODE_CONNECT_ERROR:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_evaluateinfo);
        monitorList = new ArrayList<>();
        initView();
//        initData();
        initListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.evaluateinfo_text_title));
        iv_back.setOnClickListener(this);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.evaluateinfo_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.evaluateinfo_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        adapter = new MonitorInfoPAdapter(this, monitorList, this);
        recycleView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(this);

        ll_empty = (LinearLayout) findViewById(R.id.ll_department_empty);
        tv_empty = (TextView) findViewById(R.id.empty_text);
        ll_empty.setVisibility(View.GONE);
        tv_empty.setText(R.string.noevaluateinfo_text_title);
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.getPatientAssess(pageIndex, pageSize, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back://back
                finish();
                break;

        }
    }

    @Override
    public void onItemSelect(int position) {
        PatientAssessBean bean = monitorList.get(position);
        Intent intent1 = new Intent(mContext, QuestionAssessActivity.class);
        intent1.putExtra("url", UrlAddress.H5_QUESTIONASSESS_URL + "&isEdit=0");
        intent1.putExtra("title", mContext.getResources().getString(R.string.assessment));
        intent1.putExtra("questionId", bean.getRemindId());
        intent1.putExtra("questionUserId", bean.getAnswerId());
        intent1.putExtra("patientId", patientId);
        intent1.putExtra("isFromRemind", "true");
        intent1.putExtra("isSendNurse", true);
        startActivity(intent1);

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        loading();
        loadmoreType = 1;
        pageIndex = 1;
        YiJiaHuNetWorkUtils.getPatientAssess(pageIndex, pageSize, handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        if (totalCount <= monitorList.size()) {
            toastMsg(R.string.nomore);
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        } else {
            loading();
            loadmoreType = 2;
            pageIndex++;
            YiJiaHuNetWorkUtils.getPatientAssess(pageIndex, pageSize, handler);
        }
    }
}
